<?php
namespace App\Admin\Controllers\Reimburse;

use App\Admin\Actions\Reimburse\BatchAudit;
use App\Admin\Extensions\Grid\Tool\Export;
use App\Models\Basics\AmazonCountry;
use App\Models\Basics\Currency;
use App\Models\Product\ProductClassify;
use App\Models\Reimburse\ReimburseApply;
use App\Util\ListPermission;
use App\Util\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Grid;
use Illuminate\Database\Eloquent\Collection;
use App\Admin\Renderable\Reimburse\ShowApply;
use Illuminate\Support\Facades\Request;

class AuditController extends AdminController{

    protected $title = '返款审核';

    protected function grid()
    {
        $grid = new Grid(new ReimburseApply());
        $grid->model()->where('apply_status',2)->where('audit_status',1);
        $grid = ListPermission::gridCountry($grid);
        $grid->model()->collection(function (Collection $collection){
            foreach($collection as $item) {
                $paid = [1=>null,2=>null,3=>null,4=>null];
                $paid_4 = null;
                foreach($item->paid->sortBy('order_by') as $itemPaid){
                    $paid[$itemPaid->order_by] = $itemPaid;
                    if($itemPaid->order_by == 4){
                        $paid_4 = $itemPaid;
                    }
                }
                $item->paid_status_4 = $paid_4->paid_status ?? '';
                $item->currency_code_4 = $paid_4->currency_code ?? '';
                $item->paid_method_4 = $paid_4->paid_method ?? '';
                $item->paid_account_no_4 = $paid_4->paid_account_no ?? '';
                foreach($paid as $k => $_paid){
                    $item->{'_paid_'.$k} = $_paid;
                }
                $item->check = '查看';
            }
            return $collection;
        });
        $grid->column('check','操作')->modal('付款明细',ShowApply::class);
        $grid->column('created_at','创建时间');
        $grid->column('fee_type','费用类型')->using(ReimburseApply::$_feeType);
        $grid->column('r_type','刷单类型')->using(ReimburseApply::$_rType);
        $grid->column('order_no','订单号/费用描述');
        $grid->column('country.country','国家');
        $grid->column('seller_id','销售员')->display(function ($value){
            return User::getUsernameById($value);
        });
        $grid->column('seller_sku','亚马逊SKU');
        $grid->column('sku.psku_code','公司SKU');
        $grid->column('sku.psku_cn','产品名称');
        $grid->column('skuClassify.title','品类');
        $grid->column('paid_method','支付方式')->using(ReimburseApply::$_paidMethod);
        $grid->column('paid_account_no','支付账号');
        $grid->column('paid_type','返款类型')->using(ReimburseApply::$_paidType);
        $grid->column('currency_code','币种');
        $grid->column('order_atten','订单联系人');
        $grid->column('remarks','备注');
        $grid->column('review_status','留评状态');
        $grid->column('review_link','留评链接')->view('reimburse.audit.link');
        $grid->column('review_at','留评时间');
        $grid->column('currency_code_4','佣金币种')->display(function ($value, $column){
            if($this->paid_type == 3){
                return Currency::getNameByCode($value);
            }
            return '';
        });
        $grid->column('paid_method_4','佣金支付方式')->display(function ($value, $column){
            if($this->paid_type == 3){
                return ReimburseApply::$_paidMethod[$value];
            }
            return '';
        });
        $grid->column('paid_account_no_4','佣金账号')->display(function ($value, $column){
            if($this->paid_type == 3){
                return $value;
            }
            return '';
        });
        $grid->column('_paid_1','一次性返款')->view('reimburse.audit.paid');
        $grid->column('_paid_2','首次返款')->view('reimburse.audit.paid');
        $grid->column('_paid_3','二次返款')->view('reimburse.audit.paid');
        $grid->column('_paid_4','佣金')->view('reimburse.audit.paid');

        $grid->disableActions();
        $grid->disableCreateButton();
        $grid->batchActions(function ($batch) {
            $batch->disableDelete();
            $batch->add(new BatchAudit());
        });
        $grid->fixColumns(2, -4);
        $grid->disableExport();
        $grid->tools(function (Grid\Tools $tools){
            $tools->append(new Export('reimburse/audit-download'));
        });
        $grid->paginate(10);
        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter) {
                $filter->where(function ($query) {
                    $query->where('psku_code', 'like', "%{$this->input}%")
                        ->orWhere('psku_cn', 'like', "%{$this->input}%");
                }, 'SKU','sku8')->placeholder('模糊查询SKU，名称');
                $filter->like('paid_account_no', '支付账号');
                $filter->where(function ($query) {
                    $query->whereIn('order_no', explode(',',"{$this->input}"));
                }, '订单号','orderNo');
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('fee_type','费用类型')->select(ReimburseApply::$_feeType);
                $filter->equal('r_type','刷单类型')->select(ReimburseApply::$_rType);
                $filter->equal('paid_status','付款状态')->select(ReimburseApply::$_paidStatus);
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('sku.classify_id', '品类')->select(ProductClassify::options());
                $filter->equal('country_id','国家')->select(AmazonCountry::options());
                $filter->equal('seller_id','销售')->select(User::getSellerOptions());
            });
        });
        return $grid;
    }

    public function download(){
        $request = Request::input();
        $query = $this->_initQuery($request);
        $this->_exeDownload($query);
    }

    private function _initQuery($filters){
        $query = ReimburseApply::query()->orderBy('id', 'DESC');
        $query = ListPermission::country($query);
        foreach ($filters as $key => $_filter){
            if($key == 'sku8'){
                $query->whereHas('sku', function ($query) use($_filter){
                    $query->where('psku_code', 'like', "%{$_filter}%")
                        ->orWhere('psku_cn', 'like', "%{$_filter}%");
                });
            }
            if($key == 'sku'){
                $query->whereHas('sku', function ($query) use($_filter){
                    $query->where('classify_id', $_filter['classify_id']);
                });
            }
            if($key == 'paid_account_no'){
                $query->where(function ($query) use($_filter){
                    $query->where('paid_account_no', 'like', "%{$_filter}%");
                });
            }
            if($key == 'orderNo'){
                $query->whereIn('order_no', explode(',',"{$_filter}"));
            }
            if(in_array($key,['fee_type','r_type','paid_status','country_id','seller_id'])){
                $query->where($key,$_filter);
            }
        }
        return $query;
    }

    private function _exeDownload($query){
        ReimburseApply::exeDownload($query);
    }
}
